<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/2/26
 * Time: 15:13
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Beautydiary extends Base
{
    /**
     * 美丽日记
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $start_time = input('start_time');
            $end_time = input('end_time');
            $bd_from = input('bd_from');
            $bd_status = input('bd_status');
            $bd_bdc_id = input('bd_bdc_id');
            $bd_is_recommend = input('bd_is_recommend');
            $where = [];
            // 时间
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['bd_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['bd_time'] = ['> time', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['bd_time'] = ['< time', $end_time];
            }
            // 来源
            if(isset($bd_from) && $bd_from != ''){
                $where['bd_from'] = $bd_from;
            }
            // 状态
            if(isset($bd_status) && $bd_status != ''){
                $where['bd_status'] = $bd_status;
            }else{
                $where['bd_status'] = ['NEQ', 3];
            }
            // 分类
            if(isset($bd_bdc_id) && $bd_bdc_id != ''){
                $where['bd_bdc_id'] = $bd_bdc_id;
            }
            // 推荐状态
            if(isset($bd_is_recommend) && $bd_is_recommend != ''){
                $where['bd_is_recommend'] = $bd_is_recommend;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 100;
            $count = Db::name('beauty_diary')
                ->join('up_beauty_diary_cate', 'bd_bdc_id = bdc_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('beauty_diary')
                ->join('up_beauty_diary_cate', 'bd_bdc_id = bdc_id', 'LEFT')
                ->where($where)
                ->field('bd_id, bd_from, bd_user_id, bd_status, bd_dz_number, bd_time, bdc_name, 
                bd_admin_id, bd_is_recommend, bd_comment_number')
                ->order('bd_time desc')
                ->page($Nowpage,$limits)
                ->select();
            if($lists){
                foreach($lists as $k => $v){
                    // 发布者
                    if($v['bd_from'] == 1){
                        $lists[$k]['publish_user'] = Db::table('up_u_user')->where('user_id', $v['bd_user_id'])->value('user_username');
                    }else{
                        $lists[$k]['publish_user'] = Db::table('up_admin')->where('id', $v['bd_admin_id'])->value('username');
                    }
                }
            }
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $cate = Db::table('up_beauty_diary_cate')->where('bdc_status', 1)->field('bdc_id, bdc_name')->select();
        $this->assign('cate', $cate);
        return $this->fetch();
    }
    
    /**
     * 添加美丽日记
     * @return mixed|\think\response\Json
     */
    public function diaryAdd(){
        if(Request::instance()->isPost()){
            $bd_status = input('bd_status');
            $bd_bdc_id = input('bd_bdc_id');
            $bd_word = input('bd_word');
            $bd_images = input('bd_images');
            $bd_video = input('bd_video');
            $bd_is_recommend = input('bd_is_recommend');
            
            $insert = [
                'bd_from' => 2,
                'bd_admin_id' => session('uid'),
                'bd_user_id' => 0,
                'bd_status' => $bd_status,
                'bd_bdc_id' => $bd_bdc_id,
                'bd_time' => date('Y-m-d H:i:s'),
                'bd_is_recommend' => $bd_is_recommend
            ];
            if($bd_bdc_id){
                $insert['bd_bdc_id'] = $bd_bdc_id;
            }else{
                $bd_cate = Db::table('up_beauty_diary_cate')
                    ->where(['bdc_status' => 1, 'bdc_is_default' => 2])
                    ->value('bdc_id');
                if($bd_cate){
                    $insert['bd_bdc_id'] = $bd_cate;
                }else{
                    return json(['code' => 100, 'msg' => '没有默认分类']);
                }
            }
            if($bd_word) $insert['bd_word'] = $bd_word;
            if($bd_images) $insert['bd_images'] = $bd_images;
            if($bd_video){
                // 转码
                $this->zhuanma($bd_video);
                $insert['bd_video'] = config('qiniu.domain') . 'video_' . $bd_video;
            }
            $res = Db::table('up_beauty_diary')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $cate = Db::table('up_beauty_diary_cate')->where('bdc_status', 1)->field('bdc_id, bdc_name')->select();
        $this->assign('cate', $cate);
        return $this->fetch('beautydiary/diaryadd');
    }
    
    /**
     * 美丽日记详情
     * @return mixed
     */
    public function diaryDetail(){
        $bd_id = input('bd_id');
        $diary = Db::table('up_beauty_diary')
            ->join('up_beauty_diary_cate', 'bd_bdc_id = bdc_id', 'LEFT')
            ->where('bd_id', $bd_id)
            ->field('bd_from, bd_user_id, bd_status, bd_dz_number, bd_word, bd_images, bd_video, bd_time, 
            bdc_name, bd_admin_id, bd_is_recommend, bd_comment_number')
            ->find();
        // 图集
        if($diary['bd_images']){
            $diary['bd_images'] = explode(',', trim($diary['bd_images'], ','));
        }else{
            $diary['bd_images'] = [];
        }
        // 发布者
        if($diary['bd_from'] == 1){
            $diary['publish_user'] = Db::table('up_u_user')->where('user_id', $diary['bd_user_id'])->value('user_username');
        }else{
            $diary['publish_user'] = Db::table('up_admin')->where('id', $diary['bd_admin_id'])->value('username');
        }
        
        return $this->fetch('beautydiary/diarydetail', ['diary' => $diary]);
    }
    
    /**
     * 编辑
     * @return mixed|\think\response\Json
     */
    public function diaryEdit(){
        $bd_id = input('bd_id');
        if(Request::instance()->isPost()){
            $bd_status = input('bd_status');
            $bd_bdc_id = input('bd_bdc_id');
            $bd_word = input('bd_word');
            $bd_images = input('bd_images');
            $bd_video = input('bd_video');
            
            $update = [
                'bd_status' => $bd_status,
                'bd_bdc_id' => $bd_bdc_id,
                'bd_updatetime' => date('Y-m-d H:i:s')
            ];
            if($bd_word) $update['bd_word'] = $bd_word;
            if($bd_images) $update['bd_images'] = $bd_images;
            if($bd_video){
                // 转码
                $this->zhuanma($bd_video);
                $update['bd_video'] = config('qiniu.domain') . 'video_' . $bd_video;
            }
            $res = Db::table('up_beauty_diary')->where('bd_id', $bd_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $diary = Db::table('up_beauty_diary')
            ->where('bd_id', $bd_id)
            ->field('bd_status, bd_word, bd_images, bd_video, bd_bdc_id')
            ->find();
        $cate = Db::table('up_beauty_diary_cate')->where('bdc_status', 1)->field('bdc_id, bdc_name')->select();
        return $this->fetch('beautydiary/diaryedit', ['diary' => $diary, 'bd_id' => $bd_id, 'cate' => $cate]);
    }
    
    /**
     * 评论美丽日记
     * @return mixed|\think\response\Json
     */
    public function diaryComment(){
        $bd_id = input('bd_id');
        if(Request::instance()->isPost()){
            $bdc_content = input('bdc_content');
            $diary = Db::table('up_beauty_diary')->field('bd_from, bd_user_id')->where('bd_id', $bd_id)->find();
            $insert = [
                'bdc_user_id' => 0,
                'bdc_admin_id' => session('uid'),
                'bdc_bd_id' => $bd_id,
                'bdc_content' => $bdc_content,
                'bdc_from' => 2,
                'bdc_time' => date('Y-m-d H:i:s')
            ];
            Db::startTrans();
            try{
                // 评论表
                $comment_id = Db::table('up_beauty_diary_comment')->insertGetId($insert);
                
                // 增加评论数量
                Db::table('up_beauty_diary')->where('bd_id', $bd_id)->setInc('bd_comment_number');
                
                // 如果被评论的日记是app发布的
                if($diary['bd_from'] == 1){
                    $notice = [
                        'notice_title' => '评论美丽日记',
                        'notice_range' => 2,
                        'notice_type' => 2,
                        'notice_bd_id' => $bd_id,
                        'notice_comment_id' => $comment_id,
                        'notice_reply_id' => 0,
                        'notice_reply_reply_id' => 0,
                        'notice_user' => ',' . $diary['bd_user_id'] . ',',
                        'notice_read_user' => ',0,',
                        'notice_del_user' => ',0,',
                        'notice_time' => date('Y-m-d H:i:s')
                    ];
                    Db::table('up_notice')->insert($notice);
                }
                
                Db::commit();
                return json(['code' => 200, 'msg' => '评论成功']);
            }catch(\Exception $e){
                Db::rollback();
                return json(['code' => 100, 'msg' => $e->getMessage()]);
            }
        }
        return $this->fetch('beautydiary/diarycomment', ['bd_id' => $bd_id]);
    }
    
    /**
     * 下架
     */
    public function diaryDown(){
        
    }
    
    /**
     * 上架
     */
    public function diaryOn(){
        
    }
    
    /**
     * 推荐
     * @return \think\response\Json
     */
    public function diaryRecommend(){
        $bd_id = input('id');
        $res = Db::table('up_beauty_diary')->where('bd_id', $bd_id)->setField('bd_is_recommend', 2);
        if($res){
            return json(['code' => 200, 'msg' => '推荐成功']);
        }else{
            return json(['code' => 100, 'msg' => '推荐失败']);
        }
    }
    
    /**
     * 取消推荐
     * @return \think\response\Json
     */
    public function diaryUnrecommend(){
        $bd_id = input('id');
        $res = Db::table('up_beauty_diary')->where('bd_id', $bd_id)->setField('bd_is_recommend', 1);
        if($res){
            return json(['code' => 200, 'msg' => '取消推荐成功']);
        }else{
            return json(['code' => 100, 'msg' => '取消推荐失败']);
        }
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function diaryDel(){
        $bd_id = input('id');
        $res = Db::table('up_beauty_diary')->where('bd_id', $bd_id)->setField('bd_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}